package com.seele.tigerwallet.ui.utils;

import android.content.Context;
import android.text.TextUtils;

import com.seele.tigerwallet.R;
import com.seele.tigerwallet.model.UserInfo;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.NumberFormat;

public class DataUtils {


    private static Context mContext;
    private static boolean CNY;

    public static void setContext(Context context) {
        mContext = context;
    }

    public static Context getContext() {
        return mContext;
    }


    /**
     * 资产页面获取单位
     *
     * @return
     */
    public static String getCompany() {
        if (UserInfo.get().company == 0) {
            String text = String.format(mContext.getString(R.string.assets_total), mContext.getString(R.string.usdt));
            return text;

        } else if (UserInfo.get().company == 1) {
            String text = String.format(mContext.getString(R.string.assets_total), mContext.getString(R.string.cny));
            return text;
        }
        return null;

    }

    /**
     * 资产页面获取单位
     *
     * @return
     */
    public static String getCompanySymbol() {
        if (UserInfo.get().company == 0) {
            String text = mContext.getString(R.string.usdt);
            return text;

        } else if (UserInfo.get().company == 1) {
            String text = mContext.getString(R.string.cny);
            return text;
        }
        return null;

    }


    /**
     * 资产页面获取单位
     *
     * @return
     */
    public static String getSystemCompany() {
        if (UserInfo.get().company == 0) {
            String text = mContext.getString(R.string.company_usd);
            return text;

        } else if (UserInfo.get().company == 1) {
            String text = mContext.getString(R.string.company_cny);
            return text;
        }
        return null;

    }


    /**
     * 根据 个数和单价计算总价  并根据汇率换算美元或RMB
     *
     * @param price
     * @param balance
     * @return
     */
    public static String getAssetsTotal(String price, String balance) {
        double total = 0.00f;
        if (UserInfo.get().company == 1) {
            String exchangerate = UserInfo.get().getExchangerate();
            total = ArithUtil.mul(price, balance, exchangerate);
        } else {
            total = ArithUtil.mul(price, balance);
        }
        String decimal = getTwoDecimal(total);

        return decimal;
    }


    /**
     * 获取小数点后后两位,多余舍弃,不足0补位
     *
     * @param total
     * @return
     */

    public static String getDecimal(float total) {
        if (total == 0) {
            total = 0.00000f;
        }
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        decimalFormat.setRoundingMode(RoundingMode.DOWN);

        String format = decimalFormat.format(total);
        return format;
    }


    /**
     * 获取小数点后后两位,多余舍弃,不足0补位
     *
     * @param total
     * @return
     */

    public static String getTwoDecimal(double total) {
        if (total == 0) {
            total = 0.000;
        }

        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        decimalFormat.setRoundingMode(RoundingMode.DOWN);
        String format = decimalFormat.format(total);
        return format;
    }

    public static String getFourDecimal(String total) {
        if (TextUtils.isEmpty(total)) {
            total = "0.0000";
        }

        DecimalFormat decimalFormat = new DecimalFormat("0.0000");
        decimalFormat.setRoundingMode(RoundingMode.DOWN);
        BigDecimal bigDecimal = new BigDecimal(total);
        double doubleValue = bigDecimal.doubleValue();
        String format = decimalFormat.format(doubleValue);
        return format;
    }


    public static String getFourDecimal(double total) {
        BigDecimal bigDecimal = new BigDecimal(total);
        DecimalFormat decimalFormat = new DecimalFormat("0.0000");
        decimalFormat.setRoundingMode(RoundingMode.DOWN);
        String format = decimalFormat.format(bigDecimal.doubleValue());
        return format;
    }

    public static double getDoubleValue(String value) {
        BigDecimal bigDecimal = new BigDecimal(value);
        double doubleValue = bigDecimal.doubleValue();
        return doubleValue;
    }


    public static String getSingleTokenValue(String price, String balance) {
        price = getEmptyValue(price);
        balance = getEmptyValue(balance);
        if (isCNY()) {
            String exchangerate = UserInfo.get().getExchangerate();

            return getTwoDecimal(ArithUtil.mul(price, balance, exchangerate));

        }
        return getTwoDecimal(ArithUtil.mul(price, balance));


    }


    public static String getEmptyValue(String value) {
        if (TextUtils.isEmpty(value)) {
            value = "0.00";
        }

        return value;
    }


    public static boolean isShow() {
        return SPUtils.getBoolean(SPUtils.ASSET_SHOW, false);
    }


    /**
     * 计算行情列表
     *
     * @param price
     * @return
     */
    public static String getMarketTotal(String price) {
        double total = 0;
        if (UserInfo.get().company == 1) {
            price = getEmptyValue(price);
            String exchangerate = UserInfo.get().getExchangerate();
            total = ArithUtil.mul(price, exchangerate);
            return getSixDecimal(total);
        } else if (UserInfo.get().company == 0) {
            if (TextUtils.isEmpty(price)) {
                total = 0;
            } else {
                total = Double.parseDouble(price);
            }
        }

        return getSixDecimal(total);

    }

    private static String getSixDecimal(double total) {
        DecimalFormat decimalFormat = new DecimalFormat("0.000000");
        decimalFormat.setRoundingMode(RoundingMode.DOWN);
        String format = decimalFormat.format(total);
        return format;
    }


    /**
     * 判断是美元还是RMB
     * false 美元
     *
     * @return
     */
    public static boolean isCNY() {

        return 0 == UserInfo.get().company ? false : true;
    }

    public static String getAddr(String addr) {
        if (!TextUtils.isEmpty(addr) && addr.length() > 16) {
            String start = addr.substring(0, 8);
            String end = addr.substring(addr.length() - 8, addr.length());
            return start + "....." + end;
        }

        return addr;

    }
}
